package com.adminx.dao;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.adminx.entity.AuthorityResource;

@Repository
public class AuthorityResourceDao extends BaseDao<AuthorityResource, Long>{
	
	/**
	 * 通过权限ID删除资源
	 */
	public void delResourceByAuthority(Long authorityId){
		String hql = "DELETE from AuthorityResource a where a.authorityId = "+ authorityId;
		this.getSession().createQuery(hql).executeUpdate();
	}
	
	/**
	 * 通过权限ID保存资源
	 */
	public void saveResourceByAuthorityId(String multiSelectedValue, Long authorityId){
		if(StringUtils.isNotBlank(multiSelectedValue)){
			String[] arr = multiSelectedValue.split(",");
			for (int i = 0; i < arr.length; i++) {
				AuthorityResource ar = new AuthorityResource();
				ar.setAuthorityId(authorityId);
				ar.setResourceId(Long.valueOf(arr[i]));
				this.getSession().save(ar);
			}
		}
	}
	
}
